import React, {FC, useContext} from 'react';
import "./index.less";
import {useBackendEnum} from "kf-web-library";
import {useRequest} from "ahooks";
import * as API from "@/services";
import EsSearchFilters from "@/pages/SearchPage/common/EsSearchFilters";
import {Empty, Spin} from "antd";
import HxJournalListItem from "@/components/HxJournal/HxJournalListItem";
import HxPagination from "@/components/HxPagination";
import queryString from "query-string";
import {SearchPageContext} from "@/pages/SearchPage/common/SearchPageContext";
import usePgSearch from "@/pages/SearchPage/common/usePgSearch";

interface SearchJournalProps {
}

const SearchJournal: FC<SearchJournalProps> = (props) => {
  const {enumDict, enumTypeDict, getBackendEnum} = useBackendEnum();
  const {currentSearch} = usePgSearch()
  const dataReq = useRequest((p) => {
    return API.queryFrontJournalsByEs({
      ...p,
      ...currentSearch,
    });
  }, {
    refreshDeps: [currentSearch],
  });
  const records = dataReq.data?.data || [];
  const aggs = dataReq.data?.extras?.aggs || [];

  return (
    <div className={"search-journal-container search-result-page"}>
      {
        records.length === 0 && (
          <Empty/>
        )
      }
      <div className={"search-result-page-content"}>
        <div className={"search-record-list"}>
          <Spin spinning={dataReq.loading}>
            <div className={"search-record-inner-list"}>
              {
                records.length > 0 && (
                    records.map((item, index) => (
                      <HxJournalListItem key={item.id} journal={item}/>
                    ))
                  )
              }
            </div>
            {
              dataReq.data && (
                <div className={"hx-pagination-wrapper"}>
                  <HxPagination size="default"
                                total={dataReq.data?.total}
                                current={dataReq.data.current}
                                pageSize={dataReq.data.pageSize}
                                onChange={(pageNum, pageSize) => {
                                  dataReq.run({
                                    page: pageNum,
                                    pageSize
                                  })
                                }}
                  />
                </div>
              )
            }
          </Spin>
        </div>
      </div>
    </div>
  );
};

export default SearchJournal;
